package com.LiL.Wayne.Activity.activity;

//Tabatoo
import java.io.FileInputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import android.app.Activity;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.graphics.drawable.Drawable;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnFocusChangeListener;
import android.view.View.OnLongClickListener;
import android.view.View.OnTouchListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.SeekBar;
import android.widget.TextView;

import com.LiL.Wayne.Activity.R;
import com.LiL.Wayne.Activity.R.anim;
import com.LiL.Wayne.Activity.R.drawable;
import com.LiL.Wayne.Activity.R.id;
import com.LiL.Wayne.Activity.R.layout;
import com.LiL.Wayne.Activity.fragment.MainContainerActivity;
import com.audamob.audasingers.model.Music;
import com.audamob.audasingers.model.PlayList;

public class MusicPlayer extends Activity {

	String Lyrics = "";
	static String SongName = "";
	String Album = "";
	static String Feat = "";
	MediaPlayer Mp;
	String iframe = "";
	ImageView Playe, Pause, Next;

	Boolean PopUpOnshare = false;
	PopupWindow PopUpShare;
	static Activity activity;
	static MusicPlayer MusicPlayerInstance;

	class SharePm {
		String Name;
		Drawable d;
		ResolveInfo ResolveInfo;
	}

	SeekBar seekBar;
	long progres = 0;

	boolean MODE_REPLAY = false;
	boolean MODE_SINGLE = false;
	boolean MODE_PLAYLIST = false;
	ArrayList<PlayList> PlaylistMusicList;
	ArrayList<Music> ListPlaylist = new ArrayList<Music>();
	private SimpleDateFormat formatter = new SimpleDateFormat("mm:ss");
	int currentposition = 0;
	ThreadUpdate_seekBar thUpdate;
	int mode = 1;

	static Boolean play_enbaled, pause_enabled = false;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.layout_activity_music_player_interface);
		Bundle b = getIntent().getExtras();
		MusicPlayerInstance = this;

		mode = b.getInt("mode");
		if (mode == 1) {
			SongName = b.getString("name");
			Album = b.getString("album");
			Feat = b.getString("feat");
			MainContainerActivity.Lancer_MediaPlayer(Album, mode);
		}

		if (mode == 2) {
			PlaylistMusicList = new ArrayList<PlayList>();

			try {
				PlaylistMusicList = restorePlayListList();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (ClassNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

			ListPlaylist.addAll(PlaylistMusicList.get(b.getInt("ID"))
					.getListMusic());
			SongName = ListPlaylist.get(currentposition).getName();
			Album = ListPlaylist.get(currentposition).getUrl();
			Feat = ListPlaylist.get(currentposition).getFeatring();

			MainContainerActivity.Lancer_MediaPlayer_mode_playlist(
					ListPlaylist, 0);
		}

		activity = this;
		Pause = (ImageView) findViewById(R.id.Pause);
		Playe = (ImageView) findViewById(R.id.Play);
		Next = (ImageView) findViewById(R.id.Next);
		Next.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				if (mode == 2) {
					progress_seek = false;
					MainContainerActivity.Next_MediaPlayer();
				}
			}
		});

		seekBar = (SeekBar) findViewById(R.id.PlayerSeekBar);
		seekBar.setOnTouchListener(new OnTouchListener() {
			@Override
			public boolean onTouch(View arg0, MotionEvent arg1) {
				// TODO Auto-generated method stub
				return false;
			}
		});
		seekBar.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
			}

		});
		seekBar.setOnFocusChangeListener(new OnFocusChangeListener() {

			@Override
			public void onFocusChange(View v, boolean hasFocus) {
				// TODO Auto-generated method stub

			}
		});
		seekBar.setOnLongClickListener(new OnLongClickListener() {

			@Override
			public boolean onLongClick(View arg0) {
				// TODO Auto-generated method stub
				return false;
			}
		});
		seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {

			@Override
			public void onProgressChanged(SeekBar seekBar, int progress,
					boolean fromUser) {
				progres = progress;
				Log.d("Prog", "change : " + progress);
			}

			@Override
			public void onStartTrackingTouch(SeekBar seekBar) {
			}

			@Override
			public void onStopTrackingTouch(SeekBar seekBar) {
				try {
					/*
					 * progress_seek=true; updat_UI_Seek();
					 * UpdaterProgress_seek_Thread up=new
					 * UpdaterProgress_seek_Thread(seekHandler); up.start();
					 */
					MainContainerActivity.mediaPlayer.seekTo((int) (progres
							* MainContainerActivity.mediaPlayer
									.getDuration() / 100));

				} catch (Exception e) {

				}
			}

		});

		Pause.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				if (play_enbaled && !progress_seek) {
					Pause.setVisibility(View.INVISIBLE);
					Playe.setVisibility(View.VISIBLE);
					try {

						MainContainerActivity.mediaPlayer.pause();
					} catch (Exception e) {
						// TODO: handle exception
					}
				}
			}
		});
		Playe.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				if (play_enbaled && !progress_seek) {
					Pause.setVisibility(View.VISIBLE);
					Playe.setVisibility(View.INVISIBLE);
					try {

						MainContainerActivity.mediaPlayer.start();
					} catch (Exception e) {
						// TODO: handle exception
					}
				}
			}
		});

		ImageView share = (ImageView) findViewById(R.id.Share);
		share.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View vv) {
				// TODO Auto-generated method stub

				if (!PopUpOnshare) {
					PopUpOnshare = true;
					LayoutInflater vi = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
					View v = vi.inflate(R.layout.dialog_options_share, null);
					PopUpShare = new PopupWindow(v, 400, 500);
					PopUpShare.setOutsideTouchable(false);
					PopUpShare.showAsDropDown(activity.findViewById(R.id.Share));
					ArrayList<SharePm> ListSHare = new ArrayList<SharePm>();
					ListView lv = (ListView) v.findViewById(R.id.ListShare);
					String mimeType = null;
					mimeType = "text/plain";
					Intent shareIntent = new Intent(
							android.content.Intent.ACTION_SEND);
					shareIntent.setType(mimeType);

					shareIntent.putExtra(Intent.EXTRA_TEXT, "");
					shareIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);

					final PackageManager pm = v.getContext()
							.getPackageManager();
					final List<ResolveInfo> activityList = pm
							.queryIntentActivities(shareIntent, 0);
					for (final ResolveInfo app : activityList) {
						SharePm sp = new SharePm();
						sp.d = app.loadIcon(pm);
						sp.Name = app.loadLabel(pm).toString();
						sp.ResolveInfo = app;
						ListSHare.add(sp);
						Log.d("ShareShare", " " + app.loadLabel(pm).toString());
					}
					count = ListSHare.size();
					String nom = "aymennnnn";// ImageList.get(currentIndex).getNewLocation();
					String Nomold = "zvzdv";// ImageList.get(currentIndex).getOldLocation();
					// Nomold=Nomold.substring(Nomold.lastIndexOf("."),
					// Nomold.length());

					lv.setAdapter(new ImageAdapter(ListSHare));

				} else {
					PopUpShare.dismiss();
					PopUpOnshare = false;

				}
			}
		});

		// Updater progress lyric

		// Updater progress lyric
		thUpdate = new ThreadUpdate_seekBar(handlerUpdater);
		thUpdate.start();
		Update_UI(SongName, Feat);

	}

	static int periode_attente = 0;

	public static void Update_UI(String songname, String feat) {
		SongName = songname;
		Feat = feat;
		((ImageView) MusicPlayerInstance.findViewById(R.id.Progress))
				.setVisibility(View.VISIBLE);
		((ImageView) MusicPlayerInstance.findViewById(R.id.replay))
				.setVisibility(View.VISIBLE);
		((ImageView) MusicPlayerInstance.findViewById(R.id.Play))
				.setVisibility(View.VISIBLE);
		((ImageView) MusicPlayerInstance.findViewById(R.id.Pause))
				.setVisibility(View.INVISIBLE);

		if (Build.VERSION.SDK_INT >= 16) {
			((ImageView) MusicPlayerInstance.findViewById(R.id.Play))
					.setBackground(MusicPlayerInstance.getResources()
							.getDrawable(R.drawable.play_desable));

		} else {

			((ImageView) MusicPlayerInstance.findViewById(R.id.Play))
					.setBackgroundDrawable(MusicPlayerInstance.getResources()
							.getDrawable(R.drawable.play_desable));
		}

		TextView namesong = (TextView) MusicPlayerInstance
				.findViewById(R.id.NameSong);
		TextView namesongg = (TextView) MusicPlayerInstance
				.findViewById(R.id.NameSongg);
		namesong.setText("Music : " + songname);
		namesongg.setText(songname);

		Lyric_loaded = false;
		progress_lyric = true;
		progress_player = true;
		threadLoad = MusicPlayerInstance.new ThreadLoad(handlerLoad);
		threadLoad.start();
		p_player = MusicPlayerInstance.new UpdaterProgress_player_Thread(
				progress_player_handler);
		p_player.start();
		p = MusicPlayerInstance.new UpdaterProgress_lyric_Thread(
				progressHandler);
		p.start();

	}

	static UpdaterProgress_lyric_Thread p;
	static UpdaterProgress_player_Thread p_player;
	static ThreadLoad threadLoad;

	@Override
	protected void onStart() {

		// TODO Auto-generated method stub
		super.onStart();

	}

	boolean displayed = false;

	static int compteur = 0;

	Boolean Done = false;

	int count = 0;

	public class ImageAdapter extends BaseAdapter {

		private LayoutInflater mInflater;
		ArrayList<SharePm> list;

		public ImageAdapter(ArrayList<SharePm> listSHare) {
			mInflater = (LayoutInflater) getBaseContext().getSystemService(
					Context.LAYOUT_INFLATER_SERVICE);
			list = listSHare;
		}

		public int getCount() {
			return list.size();
		}

		public Object getItem(int position) {
			return position;
		}

		public long getItemId(int position) {
			return position;
		}

		public View getView(final int position, View convertView,
				ViewGroup parent) {
			final ViewHolder holder;
			if (convertView == null) {
				holder = new ViewHolder();

				convertView = mInflater.inflate(R.layout.item_share, null);

				holder.ImageFolder = (ImageView) convertView
						.findViewById(R.id.IconAPp);
				holder.NameFolder = (TextView) convertView
						.findViewById(R.id.namAPp);

				convertView.setTag(holder);
			} else {
				holder = (ViewHolder) convertView.getTag();
			}
			convertView.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View v) {
					// TODO Auto-generated method stub
					int h = v.getId();
					startResolvedActivity(list.get(holder.id).ResolveInfo);
					// PopUpShare.dismiss();
					// PopUpOnshare=false;
					// hidecurrentImage();
				}
			});

			holder.ImageFolder.setId(position);
			holder.NameFolder.setId(position);

			int h = holder.ImageFolder.getId();

			holder.ImageFolder.setImageDrawable(list.get(h).d);

			holder.NameFolder.setText(list.get(h).Name);
			holder.id = position;
			return convertView;
		}
	}

	class ViewHolder {
		ImageView ImageFolder;
		TextView NameFolder;
		int id;

	}

	@Override
	public void onBackPressed() {
		// TODO Auto-generated method stub
		if (PopUpOnshare) {
			PopUpShare.dismiss();
			PopUpOnshare = false;
		} else {
			MainContainerActivity.Stop_MediaPlayer();
			finish();
			overridePendingTransition(R.anim.push_down_out_back,
					R.anim.push_down_in_back);
		}
	}

	private void startResolvedActivity(ResolveInfo info) {

		Intent shareIntent = new Intent(android.content.Intent.ACTION_SEND);
		String mimeType = null;
		mimeType = "text/plain";
		shareIntent.setType(mimeType);
		ArrayList<Uri> uris = new ArrayList<Uri>();
		String nom = TextLyrics.getText().toString();// ImageList.get(currentIndex).getNewLocation();

		shareIntent.putExtra(Intent.EXTRA_TEXT, nom);
		shareIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
		final Intent resolvedIntent = new Intent(shareIntent);
		ActivityInfo ai = info.activityInfo;

		resolvedIntent.setComponent(new ComponentName(
				ai.applicationInfo.packageName, ai.name));
		startActivity(resolvedIntent);

	}

	static TextView TextLyrics;

	/*******
	 * 
	 * Update SeekBar Thread
	 * 
	 */
	final Handler handlerUpdater = new Handler() {
		public void handleMessage(Message msg) {
			if (MainContainerActivity.mediaPlayer.getCurrentPosition() > (MainContainerActivity.mediaPlayer
					.getDuration() - 3 * 1000)) {

				if (mode == 1) {
					((ImageView) findViewById(R.id.Play))
							.setVisibility(View.VISIBLE);
					((ImageView) findViewById(R.id.Pause))
							.setVisibility(View.INVISIBLE);
					try {
						MainContainerActivity.mediaPlayer.pause();
						MainContainerActivity.mediaPlayer.seekTo(0);
					} catch (Exception e) {
						// TODO: handle exception
					}
				} else if (mode == 2) {
					((ImageView) findViewById(R.id.Play))
							.setVisibility(View.VISIBLE);
					play_enbaled = false;
					((ImageView) findViewById(R.id.Pause))
							.setVisibility(View.INVISIBLE);
					pause_enabled = false;
					if (Build.VERSION.SDK_INT >= 16) {
						((ImageView) findViewById(R.id.Play))
								.setBackground(activity.getResources()
										.getDrawable(R.drawable.play_desable));

					} else {

						((ImageView) findViewById(R.id.Play))
								.setBackgroundDrawable(activity.getResources()
										.getDrawable(R.drawable.play_desable));
					}

					MainContainerActivity.Next_MediaPlayer();

				}
			}

			try {
				progres = 100
						* MainContainerActivity.mediaPlayer
								.getCurrentPosition()
						/ MainContainerActivity.mediaPlayer.getDuration();
				seekBar.setProgress((int) progres);
				((TextView) findViewById(R.id.SeekStartTime)).setText(""
						+ formatter.format(MainContainerActivity.mediaPlayer
								.getCurrentPosition()));
			} catch (Exception e) {
				// TODO: handle exception
			}

		}
	};
	boolean update_seek_bar = true;

	private class ThreadUpdate_seekBar extends Thread {

		Handler mHandler;

		ThreadUpdate_seekBar(Handler h) {
			mHandler = h;
		}

		public void run() {
			while (MainContainerActivity.MediaPlayer_Connect) {
				try {
					currentThread().sleep(1000);
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				if (MainContainerActivity.MediaPlayer_Connect) {
					Message msg = mHandler.obtainMessage();
					Bundle b = new Bundle();
					b.putString("url", "");
					msg.setData(b);
					mHandler.sendMessage(msg);
				}
			}
		}
	}

	/*******
	 * 
	 * Load Lyric Thread
	 * 
	 */
	static Boolean Lyric_loaded = false;
	final static Handler handlerLoad = new Handler() {
		public void handleMessage(Message msg) {
			try {
				TextView TextLyrics = (TextView) activity
						.findViewById(R.id.TextLyrics);
				String lyric = msg.getData().toString();
				String cast = "Bundle[{url=";

				lyric = lyric.substring(cast.length() + 1, lyric.length() - 3);
				TextLyrics.setText(lyric);
				Lyric_loaded = true;
			} catch (Exception e) {
				// TODO: handle exception
			}
		}
	};

	public class ThreadLoad extends Thread {
		Handler mHandler;

		ThreadLoad(Handler h) {
			mHandler = h;
		}

		public void run() {

			try {
				currentThread().sleep(500);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			String UrlBack = "";
			try {

				String SongNamee = SongName.replace(" ", "_");
				String UrlLyrics = "";
				if (Feat.length() < 1) {
					UrlLyrics = "http://lyrics.wikia.com/Lil_Wayne:"
							+ SongNamee;
				} else {
					UrlLyrics = "http://lyrics.wikia.com/" + Feat + ":"
							+ SongNamee;
				}
				org.jsoup.nodes.Document doc = Jsoup.connect(UrlLyrics).get();
				Elements content = doc.getElementsByTag("div");
				// Elements links = content.getElementsByTag("a");
				for (Element link : content) {
					Log.d("yimchi", " dkhalna  ");
					String linkHref = link.className();
					if (linkHref.equals("lyricbox")) {
						Log.d("Lyrics", "lyrics= " + linkHref);
						String linkText = link.text();
						try {
							linkText = linkText.substring(0,
									linkText.lastIndexOf("Send "));
						} catch (Exception e) {
							// TODO: handle exception
							Log.d("yimchi",
									" c bn exception 4" + e.getMessage());
						}
						try {
							linkText = linkText.substring(0,
									linkText.lastIndexOf("</lyrics> "));

						} catch (Exception e) {
							// TODO: handle exception
							Log.d("yimchi",
									" c bn exception 3" + e.getMessage());
						}
						try {
							linkText = linkText.substring(
									linkText.indexOf("to your Cell") + 12,
									linkText.length());

						} catch (Exception e) {
							// TODO: handle exception
							Log.d("yimchi",
									" c bn exception 2" + e.getMessage());
						}
						try {
							linkText = linkText
									.substring(0, linkText
											.lastIndexOf("we are not licensed"));

						} catch (Exception e) {
							// TODO: handle exception
							Log.d("yimchi",
									" c bn exception 1 " + e.getMessage());
						}
						try {
							linkText = linkText.replace(",", "\n\n");
						} catch (Exception e) {
							// TODO: handle exception
							Log.d("yimchi", " c bn exception " + e.getMessage());
						}
						UrlBack = linkText;
						Log.d("yimchi", " c bn");
					}
				}

			} catch (Exception e) {
				// TODO: handle exception
				Log.d("yimchi", " c bn exception s7i7a  " + e.getMessage());
			}
			Log.d("yimchi", " envoye");
			Message msg = mHandler.obtainMessage();
			Bundle b = new Bundle();
			b.putString("url", "" + UrlBack);
			msg.setData(b);
			mHandler.sendMessage(msg);

		}
	}

	/*******
	 * 
	 * Progress Lyric Thread
	 * 
	 */
	static Boolean progress_lyric = true;
	private static Handler progressHandler = new Handler() {

		@Override
		public void handleMessage(Message msg) {

			if (Lyric_loaded) {
				progress_lyric = false;
				((ImageView) MusicPlayerInstance.findViewById(R.id.Progress))
						.setVisibility(View.INVISIBLE);
			} else {
				if (compteur == 0) {
					if (Build.VERSION.SDK_INT >= 16) {
						((ImageView) MusicPlayerInstance
								.findViewById(R.id.Progress))
								.setBackground(MusicPlayerInstance
										.getResources().getDrawable(
												R.drawable.progres1));

					} else {

						((ImageView) MusicPlayerInstance
								.findViewById(R.id.Progress))
								.setBackgroundDrawable(MusicPlayerInstance
										.getResources().getDrawable(
												R.drawable.progres1));
					}

					compteur = 1;
				} else if (compteur == 1) {
					if (Build.VERSION.SDK_INT >= 16) {
						((ImageView) MusicPlayerInstance
								.findViewById(R.id.Progress))
								.setBackground(MusicPlayerInstance
										.getResources().getDrawable(
												R.drawable.progres2));

					} else {

						((ImageView) MusicPlayerInstance
								.findViewById(R.id.Progress))
								.setBackgroundDrawable(MusicPlayerInstance
										.getResources().getDrawable(
												R.drawable.progres2));
					}

					compteur = 2;
				} else {
					if (Build.VERSION.SDK_INT >= 16) {
						((ImageView) MusicPlayerInstance
								.findViewById(R.id.Progress))
								.setBackground(MusicPlayerInstance
										.getResources().getDrawable(
												R.drawable.progres3));

					} else {

						((ImageView) MusicPlayerInstance
								.findViewById(R.id.Progress))
								.setBackgroundDrawable(MusicPlayerInstance
										.getResources().getDrawable(
												R.drawable.progres3));
					}
					
					compteur = 0;
				}

			}
			super.handleMessage(msg);
		}
	};

	class UpdaterProgress_lyric_Thread extends Thread {
		Handler mHandler;
		int mState;

		public UpdaterProgress_lyric_Thread(Handler h) {
			mHandler = h;
		}

		public void run() {

			while (progress_lyric) {
				try {
					currentThread().sleep(500);
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				Message msg = mHandler.obtainMessage();
				mHandler.sendMessage(msg);
			}
		}

		public void setState(int state) {
			mState = state;
		}
	}

	/*******
	 * 
	 * Progress Player Thread
	 * 
	 */

	static Boolean progress_player = true;
	int compteur_player = 0;
	private static Handler progress_player_handler = new Handler() {
		@Override
		public void handleMessage(Message msg) {

			if (MainContainerActivity.MediaPlayer_Playing && progress_player) {

				progress_player = false;

				((ImageView) MusicPlayerInstance.findViewById(R.id.replay))
						.setVisibility(View.INVISIBLE);
				play_enbaled = true;
				pause_enabled = true;
				if (MusicPlayerInstance.mode == 2) {

					// ((ImageView)MusicPlayerInstance.findViewById(R.id.Next)).setBackgroundDrawable(MusicPlayerInstance.getResources().getDrawable(R.drawable.next_enable));
				}
				
				if (Build.VERSION.SDK_INT >= 16) {
				
				((ImageView) MusicPlayerInstance.findViewById(R.id.Play))
						.setBackground(MusicPlayerInstance
								.getResources().getDrawable(
										R.drawable.play_enable));
				}else{
					((ImageView) MusicPlayerInstance.findViewById(R.id.Play))
					.setBackgroundDrawable(MusicPlayerInstance
							.getResources().getDrawable(
									R.drawable.play_enable));
					
				}
				
				if (Build.VERSION.SDK_INT >= 16) {
				((ImageView) MusicPlayerInstance.findViewById(R.id.Pause))
						.setBackground(MusicPlayerInstance
								.getResources().getDrawable(
										R.drawable.pause_enable));
				
				}else{
					((ImageView) MusicPlayerInstance.findViewById(R.id.Pause))
					.setBackgroundDrawable(MusicPlayerInstance
							.getResources().getDrawable(
									R.drawable.pause_enable));
					
				}
				
				
				((ImageView) MusicPlayerInstance.findViewById(R.id.Play))
						.setVisibility(View.INVISIBLE);
				((ImageView) MusicPlayerInstance.findViewById(R.id.Pause))
						.setVisibility(View.VISIBLE);

			} else {

				if (MusicPlayerInstance.compteur_player == 0) {
					if (Build.VERSION.SDK_INT >= 16) {
					((ImageView) MusicPlayerInstance.findViewById(R.id.replay))
							.setBackground(MusicPlayerInstance
									.getResources().getDrawable(
											R.drawable.progress_player_1));
					}else{
						((ImageView) MusicPlayerInstance.findViewById(R.id.replay))
						.setBackgroundDrawable(MusicPlayerInstance
								.getResources().getDrawable(
										R.drawable.progress_player_1));	
					}
					MusicPlayerInstance.compteur_player = 1;
				} else if (MusicPlayerInstance.compteur_player == 1) {
					if (Build.VERSION.SDK_INT >= 16) {

					((ImageView) MusicPlayerInstance.findViewById(R.id.replay))
							.setBackground(MusicPlayerInstance
									.getResources().getDrawable(
											R.drawable.progress_player_2));
					}else{
						((ImageView) MusicPlayerInstance.findViewById(R.id.replay))
						.setBackgroundDrawable(MusicPlayerInstance
								.getResources().getDrawable(
										R.drawable.progress_player_2));
					}
					MusicPlayerInstance.compteur_player = 2;
				} else {
					if (Build.VERSION.SDK_INT >= 16) {
					((ImageView) MusicPlayerInstance.findViewById(R.id.replay))
							.setBackground(MusicPlayerInstance
									.getResources().getDrawable(
											R.drawable.progress_player_3));
					}else{
						((ImageView) MusicPlayerInstance.findViewById(R.id.replay))
						.setBackgroundDrawable(MusicPlayerInstance
								.getResources().getDrawable(
										R.drawable.progress_player_3));
					}
					MusicPlayerInstance.compteur_player = 0;
				}

			}
			super.handleMessage(msg);
		}
	};

	class UpdaterProgress_player_Thread extends Thread {

		Handler mHandler;
		int mState;
		int total;

		public UpdaterProgress_player_Thread(Handler h) {
			mHandler = h;
		}

		public void run() {
			Log.d("Newt-Music", " start" + progress_player);
			while (progress_player) {
				try {
					currentThread().sleep(500);
				} catch (InterruptedException e) {
				}
				Message msg = mHandler.obtainMessage();
				mHandler.sendMessage(msg);
			}
		}

		public void setState(int state) {
			mState = state;
		}
	}

	/*******
	 * 
	 * Progress seek Thread
	 * 
	 */
	Boolean progress_seek = false;
	int seek_compt = 0;
	private Handler seekHandler = new Handler() {

		@Override
		public void handleMessage(Message msg) {

			if (MainContainerActivity.MediaPlayer_finish_seeking) {
				MainContainerActivity.MediaPlayer_finish_seeking = false;
				progress_seek = false;
				((ImageView) findViewById(R.id.replay))
						.setVisibility(View.INVISIBLE);
			} else {
				if (seek_compt == 0) {
					if (Build.VERSION.SDK_INT >= 16) {
					((ImageView) findViewById(R.id.replay))
							.setBackground(MusicPlayerInstance
									.getResources().getDrawable(
											R.drawable.progress_player_1));
					}else{
						((ImageView) findViewById(R.id.replay))
						.setBackgroundDrawable(MusicPlayerInstance
								.getResources().getDrawable(
										R.drawable.progress_player_1));
					}
					seek_compt = 1;
				} else if (seek_compt == 1) {
					if (Build.VERSION.SDK_INT >= 16) {

					((ImageView) findViewById(R.id.replay))
							.setBackground(MusicPlayerInstance
									.getResources().getDrawable(
											R.drawable.progress_player_2));
					}else{
						((ImageView) findViewById(R.id.replay))
						.setBackgroundDrawable(MusicPlayerInstance
								.getResources().getDrawable(
										R.drawable.progress_player_2));
					}
					seek_compt = 2;
				} else {
					if (Build.VERSION.SDK_INT >= 16) {
					((ImageView) findViewById(R.id.replay))
							.setBackground(MusicPlayerInstance
									.getResources().getDrawable(
											R.drawable.progress_player_3));
					}else{
						((ImageView) findViewById(R.id.replay))
						.setBackgroundDrawable(MusicPlayerInstance
								.getResources().getDrawable(
										R.drawable.progress_player_3));
					}
					seek_compt = 0;
				}
			}
			super.handleMessage(msg);
		}
	};

	public void updat_UI_Seek() {
		((ImageView) findViewById(R.id.replay)).setVisibility(View.VISIBLE);
	}

	class UpdaterProgress_seek_Thread extends Thread {
		Handler mHandler;
		int mState;

		public UpdaterProgress_seek_Thread(Handler h) {
			mHandler = h;
		}

		public void run() {

			while (progress_seek
					&& MainContainerActivity.MediaPlayer_Connect) {
				try {
					currentThread().sleep(500);
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				Message msg = mHandler.obtainMessage();
				mHandler.sendMessage(msg);
			}
		}

		public void setState(int state) {
			mState = state;
		}
	}

	/*****
	 * Restore Playlist mode==2
	 */
	public ArrayList<PlayList> restorePlayListList() throws IOException,
			ClassNotFoundException {
		FileInputStream fin = new FileInputStream(getCacheDir()
				.getAbsolutePath() + "/playlistlist");
		ObjectInputStream in = new ObjectInputStream(fin);
		ArrayList<PlayList> object = (ArrayList<PlayList>) in.readObject();
		in.close();
		return object;
	}

}
